/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.WorkPlaceService;
import com.lmx.repoDomain.tmodel.Bed;
import com.lmx.repoDomain.tmodel.CustomerServerChange;

import com.lmx.repoDomain.tmodel.Dicttype;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(CustomerServerChange.class)
@Component
public class CustomerServerChangeRepository extends BaseRepository<CustomerServerChange> {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private WorkPlaceService workPlaceService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(CustomerServerChange.class);
	}

	@Override
	@JsonApiSave
	public CustomerServerChange save(CustomerServerChange record) {

		if (record.getId() == null) {
			dslJpaService.create(record);
			if(record.getBedOld()!=null&&record.getBedNew()!=null){
				if(record.getBedOld().getId().equals(record.getBedNew().getId())){// 床位为更改
					
				}else{  
					//床位发生更改    将新的床位设置为已入住
					workPlaceService.changeDedStauts(record.getBedNew(), commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_2.label()));
				}
			}
		} else {
			if (record.getDelStatus() == 1) {// 删除

			} else {
				CustomerServerChange recordOld=customerService.getCustomerServerChange(record);
				if(!"applyStatus2".equals(recordOld.getStatus().getTypecode())){//  applyStatus2  状态  已生效
					Bed temp1=recordOld.getBedNew();
					Bed temp2=record.getBedNew();
					if(temp1!=null&&!temp1.getId().equals(temp2.getId())){
						//  先前更改的床位与现在更改的床位不一致将先前修改床位置为空闲
						workPlaceService.changeDedStauts(temp1, commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_1.label()));
					}
					if(record.getBedOld()!=null&&record.getBedNew()!=null){
						if(record.getBedOld().getId().equals(record.getBedNew().getId())){// 床位未更改
							
						}else{  
							//床位发生更改    将新的床位设置为已入住
							workPlaceService.changeDedStauts(record.getBedNew(), commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_2.label()));
						}
					}
				}
			}
			dslJpaService.update(record);
		}
		return record;
	}

}
